package com.webside.front.grabvideo.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.math3.stat.descriptive.SynchronizedMultivariateSummaryStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.webside.front.common.model.BVideo;
import com.webside.front.common.model.RComment;
import com.webside.front.common.service.BVideoService;
import com.webside.front.common.service.RCommentService;
import com.webside.front.detail.service.IDetailService;
import com.webside.front.listener.BaseData;
import com.webside.util.Common;
import com.webside.util.SensitivewordFilter;

import jodd.util.URLDecoder;

@Controller
@Scope("prototype")
@RequestMapping(value = "/comment/")
public class CommentsController {
	@Autowired
	BVideoService bVideoService;
	@Autowired
	IDetailService detailService;
	@Autowired
	RCommentService rCommentService;
	//获得父级评论
		@RequestMapping(value="recomment.html",method={RequestMethod.POST})
		@ResponseBody
		public Object showComment(@RequestParam("seriesId") Integer seriesId,@RequestParam("pageNo") Integer pageNo,HttpSession session,String youkuId) throws Exception{
			System.out.println("剧集id"+seriesId);
			long beginTime = System.currentTimeMillis();
			List<RComment> rCommentList=null;
			if(youkuId.trim().equals("")){
				rCommentList=detailService.selectRComment(seriesId,pageNo);
			}else {
				rCommentList=detailService.selectRComment(Integer.parseInt(youkuId),pageNo);
				System.out.println("来到这里了么==========================");
			}
			session.setAttribute("commentCount",rCommentList.size());
	 		SensitivewordFilter filter=new SensitivewordFilter();
			for (int i = 0; i < rCommentList.size(); i++) {
				String content= filter.replaceSensitiveWord(rCommentList.get(i).getContent(), 1, "*");
				rCommentList.get(i).setContent(content);
			}
			long endTime = System.currentTimeMillis();
			System.out.println("总共消耗时间为：" + (endTime - beginTime));
			JSONArray json1 = new JSONArray();
			json1.addAll(rCommentList);
			System.out.println(json1);
			if(json1.size()>0){
				return rCommentList;
			}
			String No_msg="";
			No_msg = "尚未评论";
			return json1;
		}
		//获得子级评论
		@RequestMapping("replyComment.html")
		@ResponseBody
		public Object showRComment(@RequestParam("seriesId") Integer seriesId,@RequestParam("pageNo") Integer pageNo,@RequestParam("parentId") Integer commentId,Model m) throws Exception{
			
			long beginTime = System.currentTimeMillis();
			List<RComment> rCommentList=detailService.selectReplyComment(seriesId ,pageNo,commentId);
			 
			int r_Count=detailService.ReplyCount(commentId);
			int cPageSize_1=r_Count%7;
			if(r_Count%7==0){
				m.addAttribute("cPageSize_1",cPageSize_1);
			}else{
				m.addAttribute("cPageSize_1",cPageSize_1+1);
			}
			SensitivewordFilter filter=new SensitivewordFilter();
			for (int i = 0; i < rCommentList.size(); i++) {
				String content= filter.replaceSensitiveWord(rCommentList.get(i).getContent(), 1, "*");
				rCommentList.get(i).setContent(content);
			}
			long endTime = System.currentTimeMillis();
			System.out.println("总共消耗时间为：" + (endTime - beginTime));
			JSONArray json = new JSONArray();
			json.addAll(rCommentList);
			System.out.println(json);
			if(json.size()>0){
				return rCommentList;
			}else{
				if(pageNo!=0){
					return "加载完毕";
				}else{
					return "尚未回复评论";
				}
			}
		    /* String	msg ="尚未回复评论";
		     JSONObject j = JSONObject.fromObject(msg);
			return "尚未回复评论";*/
		}
	//发表评论
		@RequestMapping("insertComment.html")
		@ResponseBody
		public Object insertComment(@RequestParam("seriesId") Integer seriesId,@RequestParam("parentId") Integer parentId,@RequestParam("content") String content,@RequestParam("ipCode") String ipCode,@RequestParam("childrenCount") String childrenCount,HttpServletRequest req){
			String co=URLDecoder.decode(content,"UTF-8");
			RComment comment=new RComment();
			comment.setSeriesId(seriesId);
			comment.setParentId(parentId);
			comment.setContent(co);
			
			comment.setIpcode(req.getParameter("ipCode"));
			if(childrenCount.equals("0") && childrenCount!=null){
				comment.setChildrenCount(1);
			}
			Date date=new Date();
			comment.setStatus("1");
			comment.setCreateTime(date);
			comment.setType("2");
			Object com = detailService.insertComment(comment);
			if(parentId!=0){
				//detailService.addCount(comment);
				rCommentService.updateChildrenCount(parentId);
			}
			
			return  detailService.selectRCommentByEntity(comment);
		}
}
